Identifiers.setValues   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
c 1
b 0
f 0
nc 4
nop 2
dl 0
loc 9
rs 9.6666
1
var utils = require('../utils'),
2
    Base = require('../Base');
3
4
/**
5
 * Manage the set of identifers for an object.
6
 * 
7
 * @see {@link https://github.com/FamilySearch/gedcomx/blob/master/specifications/json-format-specification.md#identifier-type|GEDCOM X JSON Spec}
8
 * 
9
 * @class
10
 * @extends Base
11
 * @param {Object} [json]
12
 */
13
var Identifiers = function(json){
14
  
15
  // Protect against forgetting the new keyword when calling the constructor
16
  if(!(this instanceof Identifiers)){
17
    return new Identifiers(json);
18
  }
19
  
20
  // If the given object is already an instance then just return it. DON'T copy it.
21
  if(Identifiers.isInstance(json)){
22
    return json;
23
  }
24
  
25
  this.init(json);
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
26
};
27
28
Base.prototype = Object.create(Base.prototype);
29
30
Identifiers._gedxClass = Identifiers.prototype._gedxClass = 'GedcomX.Identifiers';
31
32
/**
33
 * Check whether the given object is an instance of this class.
34
 * 
35
 * @param {Object} obj
36
 * @returns {Boolean}
37
 */
38
Identifiers.isInstance = function(obj){
39
  return utils.isInstance(obj, this._gedxClass);
40
};
41
42
/**
43
 * Initialize from JSON
44
 * 
45
 * @param {Object}
0 ignored issues
show
Documentation introduced by
The parameter * does not exist. Did you maybe forget to remove this comment?
Loading history...
46
 * @return {Identifiers} this
47
 */
48
Identifiers.prototype.init = function(json){
49
  
50
  Base.prototype.init.call(this, json);
51
  
52
  this.identifiers = {};
53
  
54
  if(json){
55
    this.identifiers = json;
56
    
57
    // The spec allows for types with single values to "forgo the array and use
58
    // a single string" but that's a pain to keep track of so we're going to
59
    // convert all single strings into arrays
60
    for(var a in this.identifiers){
61
      if(this.identifiers.hasOwnProperty(a) && !Array.isArray(this.identifiers[a])){
62
        this.identifiers[a] = [ this.identifiers[a] ];
63
      }
64
    }
65
  }
66
  return this;
67
};
68
69
/**
70
 * Get the values for a given type
71
 * 
72
 * @param {String=} type If not specified then values with no associated type
73
 * are returned.
74
 * @return {String[]}
75
 */
76
Identifiers.prototype.getValues = function(type){
77
  if(typeof type === 'undefined'){
78
    type = '$';
79
  }
80
  return this.identifiers[type] || [];
81
};
82
83
/**
84
 * Set the values for a given type
85
 * 
86
 * @param {String[]} values
87
 * @param {String=} type
88
 */
89
Identifiers.prototype.setValues = function(values, type){
90
  if(typeof type === 'undefined'){
91
    type = '$';
92
  }
93
  if(Array.isArray(values)){
94
    this.identifiers[type] = values;
95
  }
96
  return this;
97
};
98
99
/**
100
 * Add a value for a given type
101
 * 
102
 * @param {String} value
103
 * @param {String=} type
104
 */
105
Identifiers.prototype.addValue = function(value, type){
106
  if(typeof type === 'undefined'){
107
    type = '$';
108
  }
109
  if(!Array.isArray(this.identifiers[type])){
110
    this.identifiers[type] = [];
111
  }
112
  this.identifiers[type].push(value);
113
  return this;
114
};
115
116
/**
117
 * Export the object as JSON
118
 * 
119
 * @return {Object} JSON object
120
 */
121
Identifiers.prototype.toJSON = function(){
122
  return this.identifiers;
123
};
124
125
module.exports = Identifiers;